package com.baizhi.controller;

import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public String login(HttpServletRequest request, String username, String password, String codeuser){

        HttpSession session = request.getSession();
        String code = (String) session.getAttribute("code");
        System.out.println(code);
        System.out.println(codeuser);


        User user = userService.queryByNameAndPassword(username, password);
        session.setAttribute("user",user);

        if (user !=null && codeuser.equals(code)){

            return "redirect:/employee/findByPage";
        }

        return "redirect:/login.jsp";
    }


    /*注册*/
    @RequestMapping("register")
    public String register(HttpServletRequest request, String username, String password, String coderegister){

        HttpSession session = request.getSession();
        String code = (String) session.getAttribute("code");
        System.out.println(code);
        System.out.println(coderegister);

        User user = new User(1, username, password);
        if (user !=null && coderegister.equals(code)){
            userService.add(user);
        }

        return "redirect:/login.jsp";
    }


    /*安全退出*/
    @RequestMapping("exit")
    public String exit(HttpServletRequest request){

        HttpSession session = request.getSession();
        session.invalidate();

        return "redirect:/login.jsp";
    }

}
